package com.atguigu.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author: 叶子
 * @date: 2022/01/17 11:47
 * 本地文件写数据
 **/
public class NIOFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello,阿琳";
//        创建一个输出流->Channel
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file01.txt");
//        通过fileOutputStream获取对应的FileChannel
        FileChannel fileChannel = fileOutputStream.getChannel();//这个fileChannel真是类型是FileChannelImpl
//   创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//        将str 放入byteBuffer
        byteBuffer.put(str.getBytes());
//        对byteBuffer进行flip
        byteBuffer.flip();
//        将byteBuffer数据写入到Channel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();

    }
}